import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.util.Vector;

public class SplashConnection extends Canvas
{
	private Image icon;
	private String title;
	private MenuManager mgr;
	private SplashConnectionThread sct;
	
	public SplashConnection(MenuManager mm, String title, String url)
	{
		this.title = title;
		this.mgr = mm;
		try
		{
			this.icon = Image.createImage("/send.png");
		}
		catch(Exception e)
		{
			System.out.println("Ne morem naloziti slike.");
		}
		
		sct = new SplashConnectionThread(url, this);
	}
	
	public void callTransactionEnd()
	{
		mgr.itemSelected(FormConstants.TRANSACTION_END);
	}
	
	public void connect()
	{
		this.sct.connect();
	}
	
	public boolean error()
	{
		return sct.error();
	}
	
	public Vector getResponseHeaders()
	{
		return sct.getResponseHeaders();
	}
	
	public void cancelConnection()
	{
		sct = null;
	}
	
	public boolean isDone()
	{
		return sct.isDone();
	}
	
	public void addHeader(String name, String value)
	{
		this.sct.addHeader(name, value);
	}
	
	
	
	protected void keyReleased(int keyCode)
	{
		/* Cancel */
		mgr.itemSelected(0);
	}
	
	public void setTitle(String title)
	{
		this.title = title;
		repaint();
	}
	
	public void setIcon(Image img)
	{
		this.icon = img;
		repaint();
	}
	
	public void paint(Graphics g)
	{
		int hcenter = getWidth() / 2;
		int vcenter = getHeight() / 2;
		
		g.setColor(mgr.getDefaultBGColor());
		g.fillRect(0,0, getWidth(), getHeight());
		
		g.setColor(0x00FFFFFF - g.getColor());
		
		g.drawImage(icon, hcenter, vcenter, Graphics.VCENTER | Graphics.HCENTER);
		
		g.drawString(title, hcenter, getHeight() - 20, Graphics.BASELINE | Graphics.HCENTER);
		g.drawString("Pritisni katerokoli tipko za preklic", hcenter, 20, Graphics.BASELINE | Graphics.HCENTER);
	}
}
